﻿@model List<Gateway.Console.Models.Deployment>

@{
    ViewBag.Title = "Deployments";
}

<style>
.tooltip-inner {
    text-align:left !important;
}
</style>

<h2>Deployments</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@if (Model.Any())
{
    <div id="update-banner" class="alert alert-dismissable hidden">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        An update is available for one or more of you deployed Gateways. Click the <b>Update</b> link next to the associated deployment.
    </div>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.First().ServiceUrl)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.First().RegionSelection)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.First().InstanceCount)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.First().SelectedStorageAccount)
            </th>
            <th class="text-center">
                Https Enabled
            </th>
            <th>
                @Html.DisplayNameFor(model => model.First().DeployedVersion)
            </th>
            <th>
                Available Update
            </th>
            <th>

            </th>
        </tr>

        @foreach (var deployment in Model)
        {
            string rowClass = "";
            if (deployment.OperationInProgress)
            {
                rowClass = "warning";
            }
            <tr id="@("deployment-" + deployment.DeploymentId.ToString())" class="@rowClass">
                <td>
                    <a href="@deployment.ServiceUrl" target="_blank">@deployment.ServiceUrl</a>
                </td>
                <td>
                    @Html.DisplayFor(model => deployment.RegionSelection)
                </td>
                <td>
                    @Html.DisplayFor(model => deployment.InstanceCount)
                </td>
                <td>
                    @Html.DisplayFor(model => deployment.SelectedStorageAccount)
                </td>
                <td align="center">
                    @Html.DisplayFor(model => deployment.IsHttpsEnabled)
                </td>
                <td>
                    @Html.DisplayFor(model => deployment.DeployedVersion)
                </td>
                <td class="AvailableUpdateVersion" data-toggle="tooltip" data-container="body" data-html="true">
                    -
                </td>
                <td>
                    @if (deployment.OperationInProgress)
                    {
                        <i>Deployment in Progress</i>
                    }
                    else
                    {
                        @Html.ActionLink("Update", "UpdateVersion", new { id = deployment.DeploymentId }, new { @class = "UpdateLink" })
                        @:&nbsp;
                        @Html.ActionLink("Delete", "Delete", new { id = deployment.DeploymentId })
                    }
                </td>
            </tr>
        }
</table>
}

@section Scripts {
    <script type="text/javascript">

        $(document).ready(function () {
            var checkUpdate = @(Configuration.NoUpdateCheck ? "false" : "true");
            if (checkUpdate)
            {
                $.ajax({
                    type: "GET",
                    url: '@Url.Action("GetDeploymentUpdates")',
                    error: function (xhr, status, error) {
                    },
                    success: function (response) {
                        var highestSeverity = 0;
                        $.each(response, function (index, deployment) {
                            var alertClass = '';
                            switch (deployment.AvailableUpdate.Severity) {
                                case 3:
                                    alertClass = 'danger';
                                    break;

                                case 2:
                                    alertClass = 'warning';
                                    break;
                            }
                            if (alertClass !== '') {
                                $('#deployment-' + deployment.DeploymentId).addClass(alertClass);
                            }
                            var updateVersionCell = $('#deployment-' + deployment.DeploymentId + ' td.AvailableUpdateVersion');
                            updateVersionCell.html(deployment.AvailableUpdate.Version);
                            updateVersionCell.attr('title', deployment.AvailableUpdate.Description);
                            updateVersionCell.tooltip();

                            if (deployment.AvailableUpdate.Severity > highestSeverity) {
                                highestSeverity = deployment.AvailableUpdate.Severity;
                            }
                        });
                        if (highestSeverity > 0) {
                            var alertClass = 'alert-info';
                            switch (highestSeverity) {
                                case 3:
                                    alertClass = 'alert-danger';
                                    break;

                                case 2:
                                    alertClass = 'alert-warning'
                                    break;

                                default:
                                    alertClass = 'alert-info';
                                    break;
                            }
                            $("#update-banner").addClass(alertClass);
                            $("#update-banner").removeClass('hidden');
                        }
                    }
                });
            }
        });

</script>
}
